package com.Q01;

import java.util.Arrays;

/**
 * a字符串能否重新排序后转化b
 * @version v1.0.0
 * 创建时间：2022/6/17 13:37
 */
public class Q01_02 {
    // 排序后依次比较大小
    public boolean CheckPermutation(String s1, String s2) {
        if(s1.length()!=s2.length()) return false;
        char[] sc1 = s1.toCharArray();
        char[] sc2 = s2.toCharArray();
        Arrays.sort(sc1);
        Arrays.sort(sc2);
        return new String(sc1).equals(new String(sc2));
    }

    // 桶计数
    public boolean CheckPermutation2(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        int[] c1 = count(s1);
        int[] c2 = count(s2);
        for (int i = 0; i < c1.length; i++) {
            if (c1[i] != c2[i]) {
                return false;
            }
        }
        return true;
    }

    private int[] count(String str) {
        int[] c = new int[26];
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            c[aChar - 'a']++;
        }
        return c;
    }
}
